﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MTViewer.Commands;
using MTViewer.Models;
using PIBV;

namespace MTViewer.ViewModels
{
    public class ShowTagsViewModel : ViewModelBase 
    {
        PID_BlindingList _objBL = null;

        private string[] _pids;
        public string[] PIDs
        {
            get
            {
                return _pids;

            }
            set
            {
                if (_pids != value)
                {
                    _pids = value;
                    OnPropertyChanged("PIDs");
                }
            }
        }

        private string _selectedPID;
        public string SelectedPID
        {
            get
            {
                return _selectedPID;
            }
            set            
            {
                if (_selectedPID != value)
                {
                    _selectedPID = value;
                    ShowTagInfoForPID();
                    OnPropertyChanged("SelectedPID");
                }
            }
        }

        private double _x;
        public double X
        {
            get
            {
                return _x;
            }
            set
            {
                if (_x != value)
                {
                    _x = value;
                    OnPropertyChanged("X");
                }
            }
        }

        private double _y;
        public double Y
        {
            get
            {
                return _y;
            }
            set
            {
                if (_y != value)
                {
                    _y = value;
                    OnPropertyChanged("Y");
                }
            }
        }

        
        private double _scaleX;
        public double ScaleX
        {
            get
            {
                return _scaleX;
            }
            set
            {
                if (_scaleX != value)
                {
                    _scaleX = value;
                    OnPropertyChanged("ScaleX");
                }
            }
        }

        private double _scaleY;
        public double ScaleY
        {
            get
            {
                return _scaleY;
            }
            set
            {
                if (_scaleY != value)
                {
                    _scaleY = value;
                    OnPropertyChanged("ScaleY");
                }
            }
        }

        private double _angle;
        public double Angle
        {
            get
            {
                return _angle;
            }
            set
            {
                if (_angle != value)
                {
                    _angle = value;
                    OnPropertyChanged("Angle");
                }
            }
        }

        private TagInfoItem[] _tagInfoItems;
        public TagInfoItem[] TagInfoItems
        {
            get
            {
                return _tagInfoItems;
            }
            set
            {
                if (_tagInfoItems != value)
                {
                    _tagInfoItems = value;
                    OnPropertyChanged("TagInfoItems");
                }
            }
        }

        public ShowTagsViewModel()
        {
            string[] pdfNames = TouchObjectFactory.GetPdfFileNamesOnCanvas();

            if (_objBL == null)
            {
                _objBL = App.DbManager.CreateObject<PID_BlindingList>();
            }

            PIDs = new string[pdfNames.Length];
            for (int i = 0; i < pdfNames.Length; i++)
            {
                PIDs[i] = _objBL.GetPIDFromPDF(pdfNames[i]);
            }

            if (PIDs != null && PIDs.Count() > 0)            
                SelectedPID = PIDs[0];

            ScaleX = 1;
            ScaleY = 1;
            Angle = 0;

          

        }

        void ShowTagInfoForPID()
        {
            DataTable dt = _objBL.GetBlindingTagsForPID(SelectedPID);

            TagInfoItems = dt.AsEnumerable().Select(x => new TagInfoItem() 
                                            {
                                                Name = x.Field<string>("B_TAG_NAME"),
                                                Type = x.Field<string>("B_TAG_TYPE"),
                                                PID = x.Field<string>("B_PID")
                                            }).ToArray();
            //var tagInfoItems = 
        }

    }
}
